//! 原子操作工具
//! 
//! 提供常用的原子操作包装和工具函数

use std::sync::atomic::{AtomicUsize, AtomicU64, AtomicBool, Ordering};
use std::time::Instant;

/// 原子计数器，提供线程安全的计数功能
#[derive(Debug)]
pub struct AtomicCounter {
    value: AtomicUsize,
}

impl AtomicCounter {
    /// 创建新的原子计数器
    pub fn new(initial: usize) -> Self {
        Self {
            value: AtomicUsize::new(initial),
        }
    }

    /// 递增并返回新值
    pub fn increment(&self) -> usize {
        self.value.fetch_add(1, Ordering::Relaxed) + 1
    }

    /// 递减并返回新值
    pub fn decrement(&self) -> usize {
        self.value.fetch_sub(1, Ordering::Relaxed) - 1
    }

    /// 获取当前值
    pub fn get(&self) -> usize {
        self.value.load(Ordering::Relaxed)
    }

    /// 设置值
    pub fn set(&self, value: usize) {
        self.value.store(value, Ordering::Relaxed);
    }

    /// 比较并交换
    pub fn compare_and_swap(&self, current: usize, new: usize) -> usize {
        match self.value.compare_exchange_weak(current, new, Ordering::Relaxed, Ordering::Relaxed) {
            Ok(_) => current,
            Err(actual) => actual,
        }
    }
}

impl Default for AtomicCounter {
    fn default() -> Self {
        Self::new(0)
    }
}

/// 原子统计信息，用于性能监控
#[derive(Debug)]
pub struct AtomicStats {
    /// 总操作数
    pub total_operations: AtomicU64,
    /// 成功操作数
    pub success_operations: AtomicU64,
    /// 失败操作数
    pub error_operations: AtomicU64,
    /// 开始时间
    start_time: Instant,
    /// 是否已停止
    stopped: AtomicBool,
}

impl AtomicStats {
    /// 创建新的统计实例
    pub fn new() -> Self {
        Self {
            total_operations: AtomicU64::new(0),
            success_operations: AtomicU64::new(0),
            error_operations: AtomicU64::new(0),
            start_time: Instant::now(),
            stopped: AtomicBool::new(false),
        }
    }

    /// 记录成功操作
    pub fn record_success(&self) {
        self.total_operations.fetch_add(1, Ordering::Relaxed);
        self.success_operations.fetch_add(1, Ordering::Relaxed);
    }

    /// 记录失败操作
    pub fn record_error(&self) {
        self.total_operations.fetch_add(1, Ordering::Relaxed);
        self.error_operations.fetch_add(1, Ordering::Relaxed);
    }

    /// 获取总操作数
    pub fn total_ops(&self) -> u64 {
        self.total_operations.load(Ordering::Relaxed)
    }

    /// 获取成功率
    pub fn success_rate(&self) -> f64 {
        let total = self.total_ops();
        if total == 0 {
            return 0.0;
        }
        let success = self.success_operations.load(Ordering::Relaxed);
        success as f64 / total as f64
    }

    /// 获取每秒操作数
    pub fn ops_per_second(&self) -> f64 {
        let elapsed = self.start_time.elapsed().as_secs_f64();
        if elapsed == 0.0 {
            return 0.0;
        }
        self.total_ops() as f64 / elapsed
    }

    /// 停止统计
    pub fn stop(&self) {
        self.stopped.store(true, Ordering::Relaxed);
    }

    /// 是否已停止
    pub fn is_stopped(&self) -> bool {
        self.stopped.load(Ordering::Relaxed)
    }

    /// 重置统计
    pub fn reset(&self) {
        self.total_operations.store(0, Ordering::Relaxed);
        self.success_operations.store(0, Ordering::Relaxed);
        self.error_operations.store(0, Ordering::Relaxed);
        self.stopped.store(false, Ordering::Relaxed);
    }
}

impl Default for AtomicStats {
    fn default() -> Self {
        Self::new()
    }
}

